#include "buildInDialog.h"

buildInDialog::buildInDialog()
{
	fileBtn = new QPushButton("open");
	colorBtn = new QPushButton("color");
	fontBtn = new QPushButton("font");
	saveBtn = new QPushButton("save");
	closeBtn = new QPushButton("close");

	textEdit = new QTextEdit();


	//布局
	QVBoxLayout *vLay = new QVBoxLayout();
	QHBoxLayout *hLay = new QHBoxLayout();
	vLay->addWidget(fileBtn);
	vLay->addWidget(colorBtn);
	vLay->addWidget(fontBtn);
	vLay->addWidget(saveBtn);
	vLay->addWidget(closeBtn);

	hLay->addWidget(textEdit);
	hLay->addLayout(vLay);

	setLayout(hLay);

	connect(fileBtn, SIGNAL(clicked()), this, SLOT(fileSlot()));
	connect(colorBtn, SIGNAL(clicked()), this, SLOT(colorSlot()));
	connect(fontBtn, SIGNAL(clicked()), this, SLOT(fontSlot()));
	connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));
	connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeSlot()));
}

void buildInDialog::fileSlot()
{
	//获取文件名字
	QString str = QFileDialog::getOpenFileName(this, "打开文件", "/", "All File(*.*)");
	
	//打开文件
	QFile file(str);
	if(!file.open(QIODevice::ReadWrite))
		return;
	//得到输入流
	QTextStream in(&file);
	//读取数据
	while(!in.atEnd())
	{
		QString st = in.readLine();
		textEdit->append(st);
	}
}

void buildInDialog::colorSlot()
{
	//获取条色板
	QPalette palette = textEdit->palette();
	//打开对话框，获取颜色
	QColor color = QColorDialog::getColor(palette.color(QPalette::Text), this);

	if(color.isValid())
	{
		//将颜色放到条色板
		palette.setColor(QPalette::Window, color);
		//加载调色板
		textEdit->setPalette(palette);
	}

}

void buildInDialog::fontSlot()
{
	bool ok;
	QFont font = QFontDialog::getFont(&ok);
	if(ok)
		textEdit->setFont(font);
}

void buildInDialog::saveSlot()
{
	bool ok;
	//获取输入的信息
	QString str = QInputDialog::getText(this, "输入对话框", "请输入名字", QLineEdit::Normal, "wj", &ok);

	//根据输入的名字打开文件
	QFile file(str);
	file.open(QIODevice::WriteOnly);
	//获取输出流
	QTextStream out(&file);
	//将textEdit的内容写入到out
	out<<textEdit->toPlainText()<<"\n";
}

void buildInDialog::closeSlot()
{
	QProgressDialog *progress = new QProgressDialog();
	progress->setRange(0, 100);
	for(int i=0; i<=100; i+=10)
	{
		qApp->processEvents();
		progress->setValue(i);
		sleep(1);
	}
}
